#include "gl_head.h"
#include <direct.h>
#include <stdlib.h>

string separator("_");
string path;

// convert string to wchar_t*
void convertString(const string str,wchar_t* wstr){
	const char* cstr = str.c_str();
	size_t len = strlen(cstr) + 1;
	size_t converted = 0;
	wstr = (wchar_t*)malloc(len*sizeof(wchar_t));
	mbstowcs_s(&converted,wstr,len,cstr,_TRUNCATE);	
}

inline string getString(const int i){
	stringstream ss;
	string newstr;
	ss << i;
	ss >> newstr;
	return newstr;
}

boolean myComparator(const string &s1,const string &s2){
	s1.substr(s1.find_last_of(separator),s1.find_first_of("."));

	return atoi(s1.c_str()) < atoi(s2.c_str());
}

/*read file path from keyboard to vector<string>*/
void readFile(vector<string> &names) 
{
		cout << TIPS_INPUT << endl;
		//string path;
		//cin >> path;
		int readSize = 100;
		char * cPath = new char[readSize] ;
		cin.getline(cPath,readSize);
		path = cPath;
		cout << YOUR_INPUT << path << endl;
		string filter = "*.*";
		if(path[path.length() -1] != '\\')
		{
			path += "\\*.*";
		}

		_finddata_t fileInfo;
		long handle;
		do 
		{
			handle = _findfirst(path.c_str(),&fileInfo);
			if(handle == -1)
			{
				cout << TIPS_EXIT << endl;
				cin.getline(cPath,readSize);
				path = cPath;
				if(path == EXIT)
				{
					return;
				}

				if(path[path.length() -1] != '\\')
				{
					path += "\\*.*";
				}

			}else{
				names.push_back(fileInfo.name);
			}
			
		} while (handle == -1);

		while(!_findnext(handle,&fileInfo))
		{
			names.push_back(fileInfo.name);
		}
		
		_findclose(handle);
		//filter current directory "." and parent directory ".."
		if( names.size() > 2)
			names.erase(names.begin(),names.begin() + 2);

		path.erase(path.find("*.*"));
}



void separateNames(string separate,vector<string> &names){
	
	//traversal "names" ,then key separate by first separator
	int len = names.size();
	typedef map<string,vector<string>> NamesMap;
	typedef pair<string,vector<string>> nPair;
	NamesMap nameMap;
	NamesMap::iterator it;
	for (int i = 0; i < len; ++i)
	{
		string name = names.at(i);
		string::size_type pos = name.find(separate);
		string head = name.substr(0,pos);

		it = nameMap.find(head);

		if(it == nameMap.end())
		{
			vector<string> v;
			v.push_back(name.insert(0,path));
			nameMap.insert(nPair(head,v));
		}else
		{
			nameMap[head].push_back(name.insert(0,path));
		}

	}

	int sFirst = 1;
	for (it = nameMap.begin(); it != nameMap.end();++it)
	{
		vector<string> childNames = it->second;
		string head = it->first;
		int sSecond = 0;
		sort(childNames.begin(),childNames.end(),myComparator);
		for (vector<string>::iterator j = childNames.begin(); j != childNames.end();++j)
		{
			//rename file
			string oldName = (*j);

			cout << "fileName" << oldName << endl;

// 			string fileType = oldName.substr(oldName.find("."),oldName.size());
// 			string newName = path + head + separate + getString(sFirst) + separate + getString(sSecond) + fileType;
// 			int result = rename(oldName.c_str(),newName.c_str());
// 
// 			if(result == 0)
// 				cout << "rename before : " << oldName << "  rename after : " << newName << endl; 
// 			else
// 				cout << "rename before : " << oldName << "  rename failed!  " <<  newName <<endl;
			++sSecond;
		}
		++sFirst;
	}

// 	while(it != nameMap.end()){
// 
// 		//cout<< it->first << endl;
// 
// 		vector<string> childNames = it->second;
// 		vector<string>::iterator childIter = childNames.begin();
// 		for (int i = 0; i < childNames.size();++i)
// 		{
// 
// 			cout << *childIter << endl;
// 			childIter++;
// 		}
// 
// 		++it;
// 	}

}


int main(){
	
//  	vector<string> fileNames;
//  	readFile(fileNames);
// 	separateNames(separator,fileNames);
	//abc
	string s1("jump_1_10.txt");
	string::size_type pos = s1.find_last_of(separator);

	cout << pos << endl;

	s1 = s1.substr(s1.find_last_of(separator) + 1,2);

	cout << s1 << endl;

	getchar();
	return 0;
}